package com.medical.app.service.impl;

import com.medical.app.common.dto.ClinicRechargeDto;
import com.medical.app.common.dto.PatientCardDto;
import com.medical.app.common.dto.PatientDto;
import com.medical.app.entity.Patient;
import com.medical.app.common.dto.RechargeDto;
import com.medical.app.entity.PatientCard;
import com.medical.app.entity.Recharge;
import com.medical.app.mapper.PatientCardMapper;
import com.medical.app.mapper.PatientMapper;
import com.medical.app.mapper.RechargeMapper;
import com.medical.app.service.ClinicRechargeService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ClinicRechargeServiceImpl implements ClinicRechargeService {
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private PatientCardMapper patientCardMapper;
    @Resource
    private RechargeMapper rechargeMapper;

    @Override
    public ClinicRechargeDto getRecharge(Long patientId) {
        Patient patient = patientMapper.selectById(patientId);
        if (ObjectUtils.isEmpty(patient)) {
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        PatientCard patientCard = patientCardMapper.selectById(patient.getPatientCardNum());
        if (ObjectUtils.isEmpty(patientCard)) {
            throw new ServiceException(ResponseCode.PATIENT_CARD_SELECT_ERROR);
        }
        ClinicRechargeDto crd = new ClinicRechargeDto();
        crd.setPatientDto(BeanConvertUtils.convertTo(patient,PatientDto::new));
        crd.setPatientCardDto(BeanConvertUtils.convertTo(patientCard, PatientCardDto::new));
        return crd;
    }

    @Override
    public List<ClinicRechargeDto> getRechargeList(Long patientId) {
        List<ClinicRechargeDto> list = rechargeMapper.selectListByPatientId(patientId);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.RECHARGE_SELECT_ERROR);
        }
        return list;
    }

    @Override
    public List<ClinicRechargeDto> getListByDate(ClinicRechargeDto clinicRechargeDto) {
        Long patientId = clinicRechargeDto.getPatientDto().getPatientId();
        Date startDate = clinicRechargeDto.getStartDate();
        Date endDate = clinicRechargeDto.getEndDate();
        List<ClinicRechargeDto> list = rechargeMapper.selectListByDate(patientId,startDate,endDate);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.RECHARGE_SELECT_ERROR);
        }
        return list;
    }

    @Override
    public Integer addRecharge(RechargeDto rechargeDto) {
        //判断充值金额是否为0
        if (rechargeDto.getRechargeCount() == 0){
            throw new ServiceException(ResponseCode.RECHARGE_AMOUNT_ERROR);
        }
        int count = rechargeMapper.insert(BeanConvertUtils.convertTo(rechargeDto, Recharge::new));
        if (count <= 0){
            throw new ServiceException(ResponseCode.RECHARGE_ADD_ERROR);
        }
        return count;
    }

    @Override
    public List<PatientDto> getPatientList(Integer weichatId) {
        List<Patient> list = patientMapper.selectByWeichatId(weichatId);
        if (ObjectUtils.isEmpty(list)){
            throw new ServiceException(ResponseCode.PATIENT_NULL_ERROR);
        }
        return BeanConvertUtils.convertListTo(list,PatientDto::new);
    }
}
